from zope.interface import implements, Interface

from Products.Five import BrowserView
from Products.CMFCore.utils import getToolByName
from plone.memoize.instance import memoize
from zope.i18n.interfaces import ITranslationDomain
from zope.component import queryUtility

from my315ok.fangcan import fangcanMessageFactory as _


class IfangcanView(Interface):
    """
    fangcan view interface
    """

    def test():
        """ test method"""


class fangcanView(BrowserView):
    """
    fangcan browser view
    """
    implements(IfangcanView)

    def __init__(self, context, request):
        self.context = context
        self.request = request

    @property
    def portal_catalog(self):
        return getToolByName(self.context, 'portal_catalog')

    @property
    def portal(self):
        return getToolByName(self.context, 'portal_url').getPortalObject()

    def test(self):
        """
        test method
        """
        dummy = _(u'a dummy string')

        return {'dummy': dummy}
    @memoize
    def translates(self,item):
#        import pdb
#        pdb.set_trace()      
        
        util = queryUtility(ITranslationDomain, 'my315ok.fangcan')    
        tted = util.translate(item,target_language="zh",
                                       default="yuhu")           
        return tted
    
    @memoize
    def map(self):
        """render the map of the house position"""
        
        name = self.translates(self.context.getArea())
        out = """function initialize() {var myLatlng = new google.maps.LatLng(%s,%s);
        var myOptions = {
        zoom: 12,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
        };
       var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
       var contentString = '<div id="map-mark">' + 
       '<a target="_blank" href="#">%s</a>' + '</div><br />' +
       '<span>%s</span>';
       var infowindow = new google.maps.InfoWindow({content: contentString});
       var marker = new google.maps.Marker({position: myLatlng,map: map,title:"%s"});
       infowindow.open(map,marker);    
       }
       jq('body').bind('onload',initialize())
       """ % (self.context.getLatitude(),self.context.getLongitude(),name,self.context.getAddress(),name)
        return out
    
    @memoize
    def houseimage(self):
        """fetch house main photo"""
        
        alt = self.context.Title()
        sc = self.context.absolute_url() + '/image'
        portal = self.portal
#        import pdb
#        pdb.set_trace()
        p_url = portal.absolute_url()
        existimg = portal.unrestrictedTraverse(sc, default=None)
        if existimg == None:
            sc = p_url + "/++resource++fcwatermark.jpg"            
        html='<img src="%s" alt="%s" />' % (sc,alt)
        return html
    
    @memoize
    def addfav(self):
        """<a onclick="javascript:window.external.AddFavorite('http://www.07312s.com/sale/d-2254.html',
        ' 城郊·馨香园二手房，2室2厅2卫1阳，韶山西路上豪装好房出售 - 湘潭找房网房源-韶山西路上豪装好房出售-城郊·馨香园');
        return false;" href="http://www.07312s.com/sale/d-2254.html" class="color690">收藏房源</a>"""
        url = self.context.absolute_url()
        title = self.context.Title()
        out ="javascript:window.external.AddFavorite('%s','%s');return false;" % (url,title)
        return out
        
    




